from common.datetime_utils.datetime_tools import datetimestr_to_datetime
from common.geometry.geom import Geom

from catalog.raster import raster


def main(option):
    if option == 1:
        cid = "COPERNICUS/SENTINEL2/L2A"
        uid = "S2B_MSIL2A_20210801T173909_N0301_R098_T14TKM_20210801T213147"
        res = raster.retrieve_by_uid(
            cid, uid, asset_names=["B04", "B03", "B02"], dest_res=60
        )
        print(res.meta, res.data.shape)
    elif option == 2:
        cid = "COPERNICUS/SENTINEL2/L2A"
        aoi = Geom.from_geojson_fpath("/NAS/agriculture/china_geojson/县/原阳县.geojson")
        start_datetime = datetimestr_to_datetime("2021-07-15T00:00:00")
        end_datetime = datetimestr_to_datetime("2021-08-15T00:00:00")
        res = raster.retrieve_by_query(
            cid,
            spatial=aoi,
            temporal=(start_datetime, end_datetime),
            asset_names=["B08", "B04", "B03", "B02"],
            fpath_dest="./test2_recent_yuanyang_nrgb.tif",
            crop_to_aoi=True,
        )
        print(res)
    elif option == 3:
        cid = "COPERNICUS/SENTINEL2/L2A"
        aoi = Geom.from_geojson_fpath("/NAS/agriculture/china_geojson/县/大邑县.geojson")
        start_datetime = datetimestr_to_datetime("2019-06-12T00:00:00")
        end_datetime = datetimestr_to_datetime("2019-07-12T00:00:00")
        res = raster.retrieve_by_query(
            cid,
            spatial=aoi,
            temporal=(start_datetime, end_datetime),
            asset_names=["B08", "B04", "B03", "B02", "B07", "B09", "B12"],
            fpath_dest="./test3_dayi_s2decloud_v1.tif",
            composite_strategy="s2decloud-v1",
            crop_to_aoi=True,
        )
    elif option == 4:
        cid = "COPERNICUS/SENTINEL2/L2A"
        start_datetime = datetimestr_to_datetime("2019-06-26T00:00:00")
        end_datetime = datetimestr_to_datetime("2019-07-12T00:00:00")
        res = raster.retrieve_by_query(
            cid,
            temporal=(start_datetime, end_datetime),
            properties={"mgrs_tile": "49SER"},
            asset_names=["B08", "B04", "B03", "B02", "B07", "B09", "B12"],
            fpath_dest="./test4_49SER_s2decloud_v1.jp2",
            composite_strategy="s2decloud-v1",
        )
    elif option == 5:
        cid = "COPERNICUS/SENTINEL2/L2A"
        aoi = Geom.from_geojson_fpath("/NAS/agriculture/china_geojson/县/富裕县.geojson")
        start_datetime = datetimestr_to_datetime("2020-06-26T00:00:00")
        end_datetime = datetimestr_to_datetime("2020-07-12T00:00:00")
        res = raster.retrieve_by_query(
            cid,
            spatial=aoi,
            temporal=(start_datetime, end_datetime),
            asset_names=["B04", "B03", "B02"],
            fpath_dest="./test5_fuyu_s2decloud_v1.jp2",
            composite_strategy="s2decloud-v1",
            crop_to_aoi=True,
        )
    elif option == 6:
        cid = "ESRI/IOLULC"
        aoi = Geom.from_geojson_fpath("/NAS/agriculture/china_geojson/市/新乡市.geojson")
        start_datetime = datetimestr_to_datetime("2020-01-01T00:00:00")
        end_datetime = None
        res = raster.retrieve_by_query(
            cid,
            spatial=aoi,
            temporal=(start_datetime, end_datetime),
            asset_names=["class"],
            fpath_dest="./test6_xinxiang_iolulc.jp2",
            crop_to_aoi=True,
            dest_res=10,
        )
    elif option == 7:
        cid = "ESRI/IOLULC"
        aoi = Geom.from_geojson_fpath("/NAS/agriculture/china_geojson/省/河南省.geojson")
        start_datetime = datetimestr_to_datetime("2020-01-01T00:00:00")
        end_datetime = None
        res = raster.retrieve_by_query(
            cid,
            spatial=aoi,
            temporal=(start_datetime, end_datetime),
            asset_names=["class"],
            fpath_dest="./test7_henan_iolulc.jp2",
            crop_to_aoi=True,
            resample_alg="mode",
            dest_res=100,
        )
    elif option == 8:
        cid = "COPERNICUS/SENTINEL2/L2A"
        start_datetime = datetimestr_to_datetime("2018-01-17T00:00:00")
        end_datetime = datetimestr_to_datetime("2018-02-02T23:59:59")
        res = raster.retrieve_by_query(
            cid,
            temporal=(start_datetime, end_datetime),
            properties={"mgrs_tile": "48RVT"},
            asset_names=["B04", "B03", "B02"],
            fpath_dest="./test8_48RVT_s2decloud_v1.jp2",
            composite_strategy="s2decloud-v1",
        )


if __name__ == "__main__":
    import fire

    fire.Fire(main)
